package com.ruoyi.web.core.utils;


import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import java.io.UnsupportedEncodingException;


public class SM2Util {

    private static String privateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg8T0b63fn7QU+FU3mrELkELOXEIfwXT+7y/h5sgMr/PWgCgYIKoEcz1UBgi2hRANCAASu5nqyw8OaFlkwCCGJ814dz50G32xXe3WOcUaE5Fts+7hwFsR64Zk+raZicSlsAmNKTRIzK5JoJ3BIP6xhE8Fi";

    private static String publicKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEruZ6ssPDmhZZMAghifNeHc+dBt9sV3t1jnFGhORbbPu4cBbEeuGZPq2mYnEpbAJjSk0SMyuSaCdwSD+sYRPBYg==";


    public static SM2 getSM2() {
        return SmUtil.sm2(privateKey, publicKey);
    }


    /*
     * 公钥加密
     * */
    public static String encryptStr(String text) {

        String encryptStr = getSM2().encryptBcd(text, KeyType.PublicKey);
        return encryptStr;
    }

    /*
     * 私钥解密
     * */
    public static String decryptStr(String encryptStr) {
        String decryptStr = StrUtil.utf8Str(getSM2().decryptFromBcd(encryptStr, KeyType.PrivateKey));
        return decryptStr;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
 //       String text = "1080993347360784384/1631896559184093184/02";

//        KeyPair pair = SecureUtil.generateKeyPair("SM2");
//        byte[] privateKey = pair.getPrivate().getEncoded();
//        byte[] publicKey = pair.getPublic().getEncoded();
//        System.out.println("私钥" + new String(Base64.encodeBase64(privateKey), CharsetUtil.UTF_8));
//        System.out.println("公钥" + new String(Base64.encodeBase64(publicKey), CharsetUtil.UTF_8));
//        SM2 sm2 = SmUtil.sm2(privateKey, publicKey);
        //   String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
        // 公钥加密，私钥解密

    /*        System.out.println("text:" + encryptStr(text));

       System.out.println("text:" + decryptStr(encryptStr(text)));*/
    }
}
